﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using BusinessLayer;
using EntityModel;

namespace PL___Windows_Inteface
{
    public partial class OperationsForm : Form
    {
        private string type;
        public OperationsForm(string type)
        {
            InitializeComponent();
            this.type = type;
            if (type == "Sell")
            {
                this.Text = "Creating a new sell";
                customer_header.Text = "Select one Customer:";
                add_customer_bt.Text = "Add New Private Customer";
                procced_op_bt.Text = "Procced with the sell";
                customer_list.Columns.Add("ImageColum", 0); //Not Show
                customer_list.Columns.Add("CPR", -2, HorizontalAlignment.Left);
                customer_list.Columns.Add("Name", -2, HorizontalAlignment.Left);
                customer_list.Columns.Add("Address", -2, HorizontalAlignment.Left);
                customer_list.Columns.Add("Age", -2, HorizontalAlignment.Left);
                customer_list.Columns.Add("Phone", -2, HorizontalAlignment.Left);
                customer_list.Columns.Add("Sex", -2, HorizontalAlignment.Left);
                filllistViewPrivate();

               
                vehicle_header.Text = "Select one Car:";
                vehicle_list.Columns.Add("ImageColum", 0); //Not Show
                vehicle_list.Columns.Add("Reg Number", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Fuel", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Price", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Color", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("State", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Type", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Num of Doors", -2, HorizontalAlignment.Left);
                filllistViewCars();


            }
            else { //Leasing
                
                 
                this.Text = "Creating a Leasing";


                customer_header.Text = "Select a Customer:";
                add_customer_bt.Text = "Add New Bussiness Customer";
                procced_op_bt.Text = "Procced with the leasing";
                customer_list.Columns.Add("ImageColum", 0); //Not Show
                customer_list.Columns.Add("Company Name", -2, HorizontalAlignment.Left);
                customer_list.Columns.Add("Company Id", -2, HorizontalAlignment.Left);
                customer_list.Columns.Add("Address", -2, HorizontalAlignment.Left);
                customer_list.Columns.Add("Phone", -2, HorizontalAlignment.Left);
                customer_list.Columns.Add("Fax", -2, HorizontalAlignment.Left);
                customer_list.Columns.Add("Contact Person", -2, HorizontalAlignment.Left);
                filllistViewBusiness();

                vehicle_header.Text = "Select a Truck:";
                vehicle_list.Columns.Add("ImageColum", 0); //Not Show
                vehicle_list.Columns.Add("Reg Number", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Fuel", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Price", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Color", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("State", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Tonnage", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Num of Wheels", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("High", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Width", -2, HorizontalAlignment.Left);
                vehicle_list.Columns.Add("Large", -2, HorizontalAlignment.Left);
                filllistViewTrucks(); 
            }
        }

        
        public void filllistViewBusiness()
        {
            BL controller = new BL();
            foreach (Business b in controller.getBusinessCustomerList())
            {
                ListViewItem item = new ListViewItem(b.Customer.username);
                item.SubItems.Add(b.company_name);
                item.SubItems.Add(b.b_identifier);
                item.SubItems.Add(b.Customer.address_c);
                item.SubItems.Add(b.Customer.phone.ToString());
                item.SubItems.Add(b.fax.ToString());
                item.SubItems.Add(b.contact_person.ToString());
                customer_list.Items.Add(item);
            }
        }


        public void filllistViewTrucks()
        {
            BL controller = new BL();
            foreach (Truck t in controller.getTruckList())
            {
                ListViewItem item = new ListViewItem(t.Vehicle.reg_number);
                item.SubItems.Add(t.Vehicle.reg_number);
                item.SubItems.Add(controller.getFuelName(Convert.ToInt32(t.Vehicle.fuel)));
                item.SubItems.Add(t.Vehicle.price.ToString());
                item.SubItems.Add(t.Vehicle.colour);
                item.SubItems.Add(controller.getStateName(Convert.ToInt32(t.Vehicle.state)));
                item.SubItems.Add(t.tonnage.ToString());
                item.SubItems.Add(t.num_wheels.ToString());
                item.SubItems.Add(t.high.ToString());
                item.SubItems.Add(t.width.ToString());
                item.SubItems.Add(t.large.ToString());
                vehicle_list.Items.Add(item);
            }

        }


        public void filllistViewCars()
        {
            BL controller = new BL();
            foreach (Car c in controller.getCarList())
            {
                ListViewItem item = new ListViewItem(c.Vehicle.reg_number);
                item.SubItems.Add(c.Vehicle.reg_number);
                item.SubItems.Add(controller.getFuelName(Convert.ToInt32(c.Vehicle.fuel)));
                item.SubItems.Add(c.Vehicle.price.ToString());
                item.SubItems.Add(c.Vehicle.colour);
                item.SubItems.Add(controller.getStateName(Convert.ToInt32(c.Vehicle.state)));
                item.SubItems.Add(controller.getCarTypeName(Convert.ToInt32(c.type_car)));
                item.SubItems.Add(c.doors_number.ToString());
                vehicle_list.Items.Add(item);
            }

        }
        public void filllistViewPrivate()
        {
            BL controller = new BL();
            foreach (Private p in controller.getPrivateCustomerList())
            {
                ListViewItem item = new ListViewItem(p.Customer.username);
                item.SubItems.Add(p.cpr.ToString());
                item.SubItems.Add(p.name);
                item.SubItems.Add(p.Customer.address_c);
                item.SubItems.Add(p.age.ToString());
                item.SubItems.Add(p.Customer.phone.ToString());
                item.SubItems.Add(p.sex);
                customer_list.Items.Add(item);
            }
        }

        private void ColumnCustomerClick(object o, ColumnClickEventArgs e)
        {
            // Set the ListViewItemSorter property to a new ListViewItemComparer 
            // object. Setting this property immediately sorts the 
            // ListView using the ListViewItemComparer object.
            customer_list.ListViewItemSorter = new ListViewItemComparer(e.Column);
        }
        private void ColumnVehicleClick(object o, ColumnClickEventArgs e)
        {
            // Set the ListViewItemSorter property to a new ListViewItemComparer 
            // object. Setting this property immediately sorts the 
            // ListView using the ListViewItemComparer object.
            vehicle_list.ListViewItemSorter = new ListViewItemComparer(e.Column);
        }
        class ListViewItemComparer : IComparer
        {
            private int col;
            public ListViewItemComparer()
            {
                col = 0;
            }
            public ListViewItemComparer(int column)
            {

                col = column;
            }
            public int Compare(object x, object y)
            {
                return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
            }
        }

        private void listView_MouseDoubleClick_Customer(object sender, MouseEventArgs e)
        {
            ListView.SelectedListViewItemCollection aux = customer_list.SelectedItems;
            if (aux.Count > 0)
            {
                if (type == "Sell")
                {
                    (new ManagePrivate("VIEW", aux[0].Text)).ShowDialog();
                    customer_list.Items.Clear();
                    filllistViewPrivate();
                }
                else
                {
                    (new ManageBusiness("VIEW", aux[0].Text)).ShowDialog();
                    customer_list.Items.Clear();
                    filllistViewBusiness();
                   

                }
            }            
        }

        private void listView_MouseDoubleClick_Vehicle(object sender, MouseEventArgs e)
        {
            ListView.SelectedListViewItemCollection aux = vehicle_list.SelectedItems;
           
            if (aux.Count > 0)
            {
                if (type == "Sell")
                {
                    (new ManageCar("VIEW", aux[0].Text)).ShowDialog();
                    vehicle_list.Items.Clear();
                    filllistViewCars();
                }
                else
                {
                    (new ManageTrucks("VIEW", aux[0].Text)).ShowDialog();
                    vehicle_list.Items.Clear();
                    filllistViewTrucks();
                }
            }
        }

        private void procced_op_bt_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection aux = customer_list.SelectedItems;
            ListView.SelectedListViewItemCollection aux2 = vehicle_list.SelectedItems;
            if((aux.Count < 1)||(aux2.Count <1)){
                
            error_lb.Text ="Please select one customer and one vehicle";
            }else{
                if(type=="Sell"){
                    BL controller = new BL();
                    Private p = controller.getPrivateCustomer(aux[0].Text);
                    Car c = controller.getCar(aux2[0].Text);
                    ContractForm form = new ContractForm(contractText(p, c), "BUY");
                    if (c.Vehicle.state == 1)
                    {

                        form.ShowDialog();
                        if (form.Accepted)
                        {
                            c.Vehicle.state = 2;
                            CContract contract = new CContract(c,p, DateTime.Today);
                            controller.AddCustomerContract(contract);
                            controller.updateCar(c);
                            error_lb.ForeColor = Color.Blue;
                            error_lb.Text = "Car sold";
                        }
                    }
                    else
                    {
                        MessageBox.Show("Sorry, This car is not available anymore");
                    }
                }else{
                    BL controller = new BL();
                    Business b = controller.getBusinessCustomer(aux[0].Text);
                    Truck t = controller.getTruck(aux2[0].Text);
                    ContractForm form = new ContractForm(leasingText(b, t), "LEASE");
                    if (t.Vehicle.state==1)
                    {
                       
                        form.ShowDialog();
                        if (form.Accepted)
                        {
                            t.Vehicle.state = 3;
                            CLeasing contract = new CLeasing(t, b, form.NMonths, DateTime.Today);
                            controller.AddLeasingContract(contract);
                            controller.updateTruck(t);
                            error_lb.ForeColor = Color.Blue;
                            error_lb.Text = "Truck leased";
                        }
                    }
                    else
                    {
                        MessageBox.Show("Sorry, This car is not available anymore");
                    }
                }           
            }

        }
        private String contractText(Private customer, Car c)
        {
            BL controller = new BL();
            string fuel = controller.getFuelName((int)c.Vehicle.fuel);
            string typecar = controller.getCarTypeName((int)c.type_car);
            return "The Costumer Mr. " + customer.name + ", with address " + customer.Customer.address_c +
                   " and contact phone number " + customer.Customer.phone + " for the present contract become in the owner of a " +
                   typecar + " Car of our fleet.\n\t" + "The registration of the purchase has been done and the inspection has concluded succesfully\n\t" +
                   "Purchase details:\n\t" +
                   "Registration number: " + c.Vehicle.reg_number.ToString() + "\n\t" +
                   "Type of Fuel: " + fuel + "\n\t" +
                   "Color: " + c.Vehicle.colour.ToString() + "\n\t" +
                   "Number of doors: " + c.doors_number.ToString() + "\n\t" +
                   "The costumer declare as valid and solvent the bank details provided, and buy the car for the price of " +
                   c.Vehicle.price.ToString() + " to pay in 48 months with an interest of the 1'7%.";

        }

        private String leasingText(Business customer, Truck c)
        {
            BL controller = new BL();
            string fuel = controller.getFuelName((int)c.Vehicle.fuel);
            return "The Enterprise " + customer.company_name + ", with Se-no " + customer.b_identifier +
                   " and contact phone number " + customer.Customer.phone + " for the present contract become in the titleholder in leasing of a Truck of our fleet. " +
                   "The registration of the leasing has been done and the inspection has concluded succesfully\n\t" +
                   "Leasing details:\n\t" +
                   "Registration number: " + c.Vehicle.reg_number.ToString() + "\n\t" +
                   "Type of Fuel: " + fuel + "\n\t" +
                   "Color: " + c.Vehicle.colour.ToString() + "\n\t" +
                   "Number of wheels: " + c.num_wheels.ToString() + "\n\t" +
                   "Tonnage: " + c.tonnage.ToString() + "\n\t" +
                   "Dimensions: " + c.width + "x" + c.high + "x" + c.large + "\n\t" +
                   customer.company_name + " declare as valid and solvent the bank details provided, and get the truck leased for the price of " +
                   c.Vehicle.price.ToString() + "per month";
        }




        private void add_customer_bt_Click(object sender, EventArgs e)
        {
            if (type == "Private")
            {
                (new ManagePrivate("ADD", "")).ShowDialog();
                customer_list.Items.Clear();
                filllistViewPrivate();

            }
            else  
            {
                (new ManageBusiness("ADD", "")).ShowDialog();
                customer_list.Items.Clear();
                filllistViewBusiness();
            }
        }
    }
}
